import random
import time
import os
import uiautomator2 as u2
from Util.log import Log
from Util.read_accounts import ReadAccounts
from we_chat.add_friends_with_account import AddFriendsWithAccount

if __name__ == "__main__":

    currentAbsolutPath = os.getcwd()
    accountFilePath = os.path.join(currentAbsolutPath, "data/7.xls")
    readAccount = ReadAccounts()

    try:
        # 链接设备
        d = u2.connect()
        # 设备解锁
        # d.unlock()
        # 停用UiAutomator的守护程序
        d.service("uiautomator").stop()
        # 检查并维持设备端守护进程处于运行状态
        d.healthcheck()
        d.debug = False

        addFriends = AddFriendsWithAccount(d)
        if addFriends.showAddFriendsWindow():
            #加载微信号
            readAccount.load(accountFilePath)

            for lineData in readAccount:

                time.sleep(1)
                #     d.watchers.remove();

                Log.debug("开始添加微信号：" + lineData.account)
                if addFriends.activeSearchAccountFiled():
                    time.sleep(1)
                    # 循环读取微信号
                    if addFriends.searchFriendsAccount(lineData.account):
                        time.sleep(2)

                        result = addFriends.addContact(lineData)
                        if result == 0 or result == 1 or result == 2:
                            # 加完一个，随机休眠
                            sleepTime = random.randint(3, 30)
                            time.sleep(sleepTime)
                        else:
                            break
                    else:
                        break
                else:
                    break
    finally:
        # readAccount.close()
        pass

